package com.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mapper.FriendMapper;
import com.mapper.SysUserMapper;
import com.mapper.UserInfoMapper;
import com.pojo.dto.Message;
import com.pojo.entity.CFriends;
import com.pojo.entity.SysUserInfo;
import com.pojo.vo.CFriendVo;
import com.service.CFriendService;
import com.util.PojoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CFriendServiceImpl implements CFriendService {
    @Autowired
    FriendMapper friendMapper;
    @Autowired
    SysUserMapper userMapper;

    @Override
    public Message<String> applyFriend(CFriendVo cFriendVo) {
        if (cFriendVo.getFriendname() != null) {
            if (getFriendId(cFriendVo) != null)
                cFriendVo.setFriendId(getFriendId(cFriendVo));
            else return Message.errorMessage("0", "该用户不存在！");
        } else if (cFriendVo.getFriendId() != null) {
            cFriendVo.setFriendId(cFriendVo.getFriendId());
        }
        cFriendVo.setUserId(cFriendVo.getUserId());
        CFriends cFriends = PojoUtil.copyObject(cFriendVo, CFriends.class);
        int insert = friendMapper.insert(cFriends);
        if (insert > 0) return Message.successMessage((long) insert, "已发送好友申请");
        return Message.errorMessage("0", "发送好友申请失败");
    }

    @Override
    public Message<String> addFriend(CFriends cFriends) {
        cFriends.setState(2);
        int update = friendMapper.update(cFriends, null);
        if (update > 0) return Message.successMessage((long) update, "添加好友成功");
        return Message.errorMessage("0", "添加好友失败");
    }

    @Override
    public Message<String> updateFriend(CFriends cFriends) {
        return null;
    }

    @Override
    public Message<String> deleteFriend(CFriends cFriends) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("userId", cFriends.getUserId());
        wrapper.eq("friendId", cFriends.getFriendId());
        int delete = friendMapper.delete(wrapper);
        if (delete > 0) return Message.successMessage((long) delete, "删除好友成功");
        return Message.errorMessage("0", "删除好友失败");

    }

    @Override
    public Message<CFriendVo> selectFriend(CFriendVo cFriends) {

        List<Long> friendIds = friendMapper.getFriendIds(cFriends.getUserId());
        if (cFriends.getFriendname() != null) {
            if (getFriendId(cFriends) != null) {
                cFriends.setFriendId(getFriendId(cFriends));
                cFriends.setUsername(getUserName(cFriends.getUserId()));
                return Message.successMessage((long) 1, cFriends);
            }
            else return Message.errorMessage("0", "该用户不存在！");
        } else if (friendIds.contains(cFriends.getFriendId())) {
            cFriends.setUsername(getUserName(cFriends.getUserId()));
            cFriends.setFriendname(getFriendName(cFriends.getFriendId()));
            return Message.successMessage((long) 1, cFriends);
        }
        return Message.errorMessage("0", "查询好友失败");
    }

    @Override
    public Message<List<CFriendVo>> selectFriends(CFriends cFriends) {
        List<Long> friendIds = friendMapper.getFriendIds(cFriends.getUserId());
        List<CFriendVo> friendVos = new ArrayList<>();
        for (int i = 0; i < friendIds.size(); i++) {
            CFriendVo cFriendVo = PojoUtil.copyObject(friendIds.get(i), CFriendVo.class);
            cFriendVo.setUsername(getUserName(cFriends.getUserId()));
            cFriendVo.setFriendname(getFriendName(friendIds.get(i)));
            friendVos.add(cFriendVo);
        }
        return Message.successMessage((long) friendVos.size(), friendVos);

    }


    public String getUserName(Long userId) {

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("id", userId);
        return userMapper.selectOne(wrapper).getUsername();
    }

    public String getFriendName(Long friendId) {

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("id", friendId);
        return userMapper.selectOne(wrapper).getUsername();

    }

    public Long getFriendId(CFriendVo cFriendVo) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username", cFriendVo.getFriendname());
        //wrapper.eq("userId",cFriendVo.getUserId());
        return userMapper.selectOne(wrapper).getId();
    }
}
